#include <iostream>
using namespace std;

void printArr(int arr[])
{
    for (int i = 0; arr[i]; i++) printf("%d ", arr[i]);
    puts("");
}

void repay(int arr[], int l, int r, int m)
{
    int i,j,k;
    int n1 = m-l+1,n2= r-m;
    int a[n1],b[n2];
    for ( i = 0; i <= n1; i++) a[i] = arr[i+l];
    for ( i = 0; i <= n2; i++) b[i] = arr[i+m+1];

    for (i=0,j=0,k = l; k < r+1; k++)
    {
        if(i>=n1) arr[k]=b[j++];
        else if(j>=n2) arr[k]=a[i++];
        else if(a[i]<b[j]) arr[k] = a[i++];
        else arr[k]=b[j++];
    }
    
}

void merge_sort(int arr[], int l, int r)
{
    if (l < r)
    {
        int mid = (l + r) / 2;
        merge_sort(arr, l, mid);
        merge_sort(arr, mid + 1, r);
        repay(arr, l, r, mid);
    }
}

void merge(int arr[], int len)
{
    merge_sort(arr, 0, len - 1);
}

int arr[12] = {2, 1, 5, 7, 1, 4, 6, 2, 7, 2, 67, 32};
int main(int argc, char const *argv[])
{
    int n = sizeof(arr) / sizeof(arr[0]);
    printArr(arr);
    merge(arr, n);
    printArr(arr);
    return 0;
}
